package com.cykj.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.cykj.annoation.Log;
import com.cykj.dto.ResponseDTO;
import com.cykj.service.IAdminService;
import com.cykj.util.RedisServiceUtil;
import com.cykj.util.ValidateCodeUtil;
import com.cykj.vo.AdminVO;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.UUID;

/**
 * @Program: Mooc-Admin
 * @Package: com.cykj.controller
 * @Author: 张俊杰
 * @Create: 2025-07-08 20:40
 * @Description: TODO
 * @Vision: 1.0
 **/
@RestController
@RequestMapping("system")
public class AdminLoginController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private ValidateCodeUtil validateCodeUtil;

    @Autowired
    private RedisServiceUtil redisServiceUtil;

    /**
     * @Description:更改密码
     * @Param:[adminVO]
     * @Return:com.cykj.dto.ResponseDTO
     */
    @Operation(summary = "更换密码")
    @Log(model = "登录模块", value = "更换密码")
    @PutMapping("update")
    public ResponseDTO updatePassword(@RequestBody AdminVO adminVO) {
        return adminService.updatePassword(adminVO);
    }


    /**
     * @Description:获取用户信息
     * @Param:[]
     * @Return:com.cykj.dto.ResponseDTO
     */
    @Operation(summary = "获取用户信息")
    @Log(model = "登录模块", value = "获取用户信息")
    @GetMapping("getInfo")
    public ResponseDTO getInfo() {
        SaSession session = StpUtil.getSession();
        String name = session.getString("username");
        Integer roleId = session.getInt("roleId");
        String account = session.getString("account");
        Integer userId = session.getInt("userId");
        String avatar = session.getString("avatar");

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("roleId", roleId);
        hashMap.put("account", account);
        hashMap.put("userId", userId);
        hashMap.put("avatar", avatar);
        return ResponseDTO.success(hashMap);
    }

    /**
     * @Description:登录
     * @Param:[adminVO]
     * @Return:com.cykj.dto.ResponseDTO
     */
    @Operation(summary = "登录")
    @Log(model = "登录模块", value = "登录")
    @PostMapping("login")
    public ResponseDTO login(@RequestBody AdminVO adminVO) {
        //System.out.println(adminVO);
        return adminService.adminLogin(adminVO);
    }

    /**
     * @Description:获取图形验证码
     * @Param:[uniqueCode]
     * @Return:com.cykj.dto.ResponseDTO
     */
    @Operation(summary = "获取验证码")
    @Log(model = "登录模块", value = "获取验证码")
    @GetMapping("getValidate")
    public ResponseDTO getValidate(String uniqueCode) {
        // 删除多余的验证码数据
        if (uniqueCode != null) {
            redisServiceUtil.delete(uniqueCode);
            //System.out.println("删除掉缓存的数据");
        }
        // 2. 获取图形验证码
        String randomCodeImage = validateCodeUtil.getRandomCodeImage();
        // 3. 获取验证码
        String code = validateCodeUtil.getCode();
        // 获取唯一标识
        String unique = UUID.randomUUID().toString().replace("-", "");
        // 把验证码存储到redis缓存当中
        redisServiceUtil.set(unique, code);

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("code", randomCodeImage);
        hashMap.put("unique", unique);
        return ResponseDTO.success(hashMap);
    }

}
